<?php
namespace bee;

define("MSG_LEVEL_NORMAL",       1);
define("MSG_LEVEL_WARNING",      2);
define("MSG_LEVEL_ERROR",        4);
define("MSG_LEVEL_FATAL",        8);


final class app extends singleton
{ protected static $__instance;
  protected  $time;
/**
 * @var user
 */
  protected $user;
  protected $MessageText;
  protected $MessageLevel;
  protected $DebugText;
  protected $debugVars = array();
  protected $timePoints = array();
  public    $activeDebugTab;

  protected $kom_filter;
  protected $publicFrom;
  protected $curentForm;
  protected $template;
  public    $activeMenu;
  protected $formType;
  public    $echo;
  public    $tmpDoc;
  protected $startTime;

  protected $errorCode;
  protected $errorDescription;

  /**
   * @var oArray
   */
  public $request;

  public function getTemplate()
  { return $this->template;
  }

  public function setTemplate($template)
  { $this->template = $template;
  }

  public function isPublicForm()
  { return $this->publicFrom;
  }

  public function getCurentForm()
  { return $this->curentForm;
  }

  public function getClientFilter()
  { return $this->kom_filter;
  }

  public function getFormType()
  { return $this->formType;
  }

  public function getTime()
  { return $this->time;
  }
  /**
   * @return user
   */
  public function getUser()
  { return $this->user;
  }
  public function setDebug($text, $append = true)
  { if($append)
    { $this->DebugText .= ($this->DebugText ? " / " : "") . $text;
    } else
    { $this->DebugText = $text;
    }
  }

  public function getDebug()
  { return $this->DebugText;
  }

  public function addDebugVar($varName, $varValue)
  {
    $this->debugVars[$varName] =  $varValue;
  }

  public function getDebugVars()
  {
    return $this->debugVars;
  }

  public function addTimePoint($timePointName)
  {
    $this->timePoints[$timePointName] = microtime(true) - $this->startTime;
  }

  public function getTimePoints()
  {
  	return $this->timePoints;
  }

  public function setMessage($text, $level = MSG_LEVEL_NORMAL, $reset = true)
  { if($text)
    { $this->MessageText = ($reset ? "" : $this->MessageText . "\n ") . $text;
    }
    if($level > $this->MessageLevel)
    { $this->MessageLevel = $level;
    }
  }

  public function setError($code, $description)
  {
    $this->errorDescription = $description;
    $this->errorCode = $code;
  }

  public function getErrorCode()
  {
    return $this->errorCode;
  }

  public function getErrorDescription()
  {
    return $this->errorDescription;
  }

  public function getMessageText()
  { return $this->MessageText;
  }

  public function getMessageLevel()
  { switch ($this->MessageLevel)
    { case 1:
        return "note";
      case 2:
        return "warning";
      case 4:
        return "error";
      case 8:
        return "fatal";
      default;
        return "normal";
    }
  }

  public function getLevelFromStyle($style)
  { switch($style)
    { case "note":
        return MSG_LEVEL_NORMAL;
      case "warning":
        return MSG_LEVEL_WARNING;
      case "error":
        return MSG_LEVEL_ERROR;
      case "fatal":
        return MSG_LEVEL_FATAL;
      default:
        return 0;
    }
  }

  /**
   * @return app
   */
  public static function getInstance()
  { if(!(static::$__instance instanceof app))
    { static::$__instance = new app();
      //print_r(static::$request);
    }
    return static::$__instance;
  }

  public function clearUser()
  {
    $this->user->clear();

  }

  protected function __construct()
  {

    //nl2br(print_r(debug_backtrace()));
    $this->user = new user();
    if(isset($_SESSION["username"]) || isset($_REQUEST["log_username"]))
    { $this->user = user::getByLogin(isset($_SESSION["username"]) ? $_SESSION["username"] :
        (isset($_REQUEST["log_username"]) ? $_REQUEST["log_username"] : ""));
      if($this->user)
      {
        $this->kom_filter = $this->user->view_rights == 2 ? 0 : $this->user->org_id;
      }
    }

    global                     $startTime;
    $this->startTime           = $startTime;
    $toSession                 = false;
    if(isset($_REQUEST["__form"]) && $_REQUEST["__form"])
    { $this->curentForm        = $_REQUEST["__form"];
      $this->formType          = PAGE_TYPE_FORM;
      $toSession               = true;
    } else if(isset($_REQUEST["sys_page"]))
    { $this->curentForm        = $_REQUEST["sys_page"];
      $this->formType          = PAGE_TYPE_PAGE;
      $toSession               = true;
    } else if(isset($_REQUEST["special_page"]))
    { $this->curentForm        = $_REQUEST["special_page"];
      $this->formType          = PAGE_TYPE_SPECIAL;
    } else if(isset($_REQUEST["special_app_page"]))
    { $this->curentForm        = $_REQUEST["special_app_page"];
      $this->formType          = PAGE_TYPE_SPECIAL_APP;
    } else if(isset($_SESSION["__form"]) && $_SESSION["__form"])
    { $this->curentForm        = $_SESSION["__form"];
      $this->formType          = $_SESSION["__formType"];
      $toSession               = true;
    } else
    { $this->curentForm        = config::getInstance()->sys_START_PAGE;
      $this->formType          = config::getInstance()->sys_START_PAGE_TYPE;
      $toSession               = true;
    }
    if($toSession)
    { $_SESSION["__form"]        = $this->curentForm;
      $_SESSION["__formType"]    = $this->formType;
    }
    //die($this->curentForm);
    $gr_task = new gr_task();
    $gr_task->sys_page = $this->curentForm;
    $gr_task->per_den = 1;
    $gr_task->group_id = -2;
    $rs = new rowSet($gr_task);

    if($rs->getRow($gr_task))
    { $this->publicFrom = true;
    }
//echo $this->curentForm;
    if(!isset($_REQUEST["__template"]))
    {
      if(!isset($_SESSION["__template"]))
      {
        $this->template = config::getInstance()->sys_DEFAULT_TEMPLATE;
      } else
      {
        $this->template = $_SESSION["__template"];
      }
    } else
    { $this->template = $_REQUEST["__template"];
      $_SESSION["__template"] = $_REQUEST["__template"];
    }

    $this->activeMenu = $this->curentForm;
    //die(print_r($_REQUEST));
    $this->request =  new oArray($_REQUEST);
    //die(print_r($this->request));
    $this->time = date("Y-m-d H:i:s");
    static::setMessage($this->request->getField("sys_msg"), $this->getLevelFromStyle($this->request->getField("sys_msg_style")));

  }
  public function getStartTime()
  { return $this->startTime;
  }
  public function getExecutionTime()
  {     return (microtime(true) - $this->startTime);
  }
}